<template>
  <el-dialog
    title="修改设备类型参数"
    :visible.sync="dialogVisible"
    width="600px"
  >
    <el-form
      @keyup.enter.native="update"
      ref="editDataForm"
      :model="dataForm"
      :label-width="formLabelWidth"
      :show-message="false"
    >
      <el-form-item
        prop="name"
        label="名称"
        required
      >
        <el-input
          class="input"
          v-model="dataForm.name"
          auto-complete="off"
          placeholder="请输入设备参数名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="displayName"
        label="显示名称"
        required
      >
        <el-input
          class="input"
          v-model="dataForm.displayName"
          auto-complete="off"
          placeholder="请输入设备参数显示名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="unit"
        label="单位"
      >
        <el-input
          class="input"
          v-model="dataForm.unit"
          auto-complete="off"
          placeholder="请输入设备参数单位"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="description"
        label="描述"
      >
        <el-input
          class="input"
          v-model="dataForm.description"
          auto-complete="off"
          placeholder="请输入设备参数描述"
        ></el-input>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button
        type="primary"
        @click="update"
        :loading="btnLoading"
      >确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { validateForm } from './service';
import { Message } from 'element-ui';
import { parseError } from '../../../utils';
import { updateDeviceParameterApi } from '../../../api/deviceParameter';

export default {
  data() {
    this.id = '';
    this.formLabelWidth = '120px';
    return {
      dialogVisible: false,
      btnLoading: false,
      dataForm: {
        name: '',
        displayName: '',
        unit: '',
        description: ''
      }
    };
  },
  watch: {
    dialogVisible: {
      handler(value) {
        if (!value) {
          this.$refs.editDataForm.resetFields();
          this.id = '';
        }
      }
    }
  },
  methods: {
    showDialog(id, name, displayName, unit, description) {
      this.id = id;
      Object.assign(this.dataForm, { name, displayName, unit, description });
      this.dialogVisible = true;
    },
    update() {
      const validError = validateForm(this.dataForm);
      if (validError) return Message.warning(validError);
      this.btnLoading = true;
      updateDeviceParameterApi(
        this.id,
        this.dataForm.name,
        this.dataForm.displayName,
        this.dataForm.unit,
        this.dataForm.description
      )
        .then(res => {
          Message.success('更新成功');
          this.dialogVisible = false;
          this.$emit('refresh');
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.btnLoading = false));
    }
  }
};
</script>

<style lang="scss" scoped>
.input {
  width: 300px;
}
</style>
